package com.qingzhu.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qingzhu.pojovo.ResultVo;
import com.qingzhu.service.PayService;
import com.qingzhu.service.impl.PayServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/pay")
public class PayController extends HttpServlet {
    PayService payService = new PayServiceImpl();
    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("金额支付服务..");

        String orderId = req.getParameter("orderId");
        String sunShopPrice = req.getParameter("sunShopPrice");
        System.out.println("订单号："+orderId+" 总金额："+sunShopPrice);
        ResultVo resultVo = null;
        if (orderId != null) {
            Map<String, String> map = payService.toPay(orderId, sunShopPrice);
            resultVo = new ResultVo(200,map);
        }else {
            resultVo = new ResultVo(1002,"未接收到订单信息");
        }
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
}
